package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.Category;
import com.ruoyi.business.mapper.CategoryMapper;
import com.ruoyi.business.service.ICategoryService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * CategoryService业务层处理
 *
 * @author win
 * @date 2021-06-14
 */
@Service
public class CategoryServiceImpl implements ICategoryService
{
    @Autowired
    private CategoryMapper categoryMapper;

    @Resource
    private IdWorker idWorker;

    /**
     * 查询Category
     *
     * @param id CategoryID
     * @return Category
     */
    @Override
    public Category selectCategoryById(Long id)
    {
        return categoryMapper.selectCategoryById(id);
    }

    /**
     * 查询Category列表
     *
     * @param category Category
     * @return Category
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增Category
     *
     * @param category Category
     * @return 结果
     */
    @Override
    public int insertCategory(Category category)
    {
        category.setId(idWorker.nextId());
        category.setCreateTime(DateUtils.getNowDate());
        category.setCreateBy(SecurityUtils.getUsername());
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改Category
     *
     * @param category Category
     * @return 结果
     */
    @Override
    public int updateCategory(Category category)
    {
        category.setUpdateTime(DateUtils.getNowDate());
        category.setUpdateBy(SecurityUtils.getUsername());
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除Category
     *
     * @param ids 需要删除的CategoryID
     * @return 结果
     */
    @Override
    public int deleteCategoryByIds(Long[] ids)
    {
        return categoryMapper.deleteCategoryByIds(ids);
    }

    /**
     * 删除Category信息
     *
     * @param id CategoryID
     * @return 结果
     */
    @Override
    public int deleteCategoryById(Long id)
    {
        return categoryMapper.deleteCategoryById(id);
    }

    @Override
    public Category queryTree(List<Category> allList, Category parent) {
        if (parent != null) {
            List<Category> children = allList.stream().filter(item -> item.getParentId().equals(parent.getId())).collect(Collectors.toList());
            if (children.size() > 0) {
                children.forEach(item -> queryTree(allList, item));
                parent.setChildren(children);
            } else {
                parent.setLeaf(true);
            }
            return parent;
        }
        return null;
    }

    @Override
    public Integer countCategories(Integer position){
        return categoryMapper.countCategories(position);
    }

    @Override
    public List<Category> queryChildrenList(List<Category> allList, List<Category> childrenList, Category parentCategory){
        if ( parentCategory != null ) {
            List<Category> children = allList.stream().filter(item -> item.getParentId().equals(parentCategory.getId())).collect(Collectors.toList());
            if (children.size() > 0) {
                childrenList.addAll(children);
                children.forEach(item -> queryChildrenList(allList, childrenList, item));
            }
        }
        return null;
    }

    @Override
    public void queryParentNames(List<Category> allList, Category category, ArrayList<Map<String, Object>> names) {
//        names.add(category.getTitle());
        HashMap<String, Object> cate = new HashMap<>();
        cate.put("id", category.getId().toString());
        cate.put("name", category.getTitle());

        names.add(cate);

        allList.forEach(item -> {
            if (item.getId().equals(category.getParentId())) {
                queryParentNames(allList, item, names);
            }
        });
    }
}
